﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Vanfa.Entities.Album;
using Vanfa.Biz.Album;
using Vanfa.Common;
using Vanfa.Web.UI;

namespace Vanfa.Web
{
    public partial class work : BasePage
    {
        private const string photoVPath = "/upload/photo";
        private const string thumbVPath = "/upload/thumbnail";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.Enctype = "multipart/form-data";
            if (!Page.IsPostBack)
            {
                LoadUserPhotoes(UserData.UserID);
                return;
            }
            var p = this.SetUserPhoto();
            if (p == null)
            {
                Toolkit.MessageBox("请选择上传照片！", this);
                return;
            }
            UserPhotoMag.Upload(p);
            LoadUserPhotoes(UserData.UserID);
        }

        private vUserPhoto SetUserPhoto()
        {
            var file = Request.Files["fpPhoto"];
            if (file == null)
                return null;
            //保存原图
            var newFname = "show" + DateTime.Now.ToString("yyMMddHHmmss");
            FileInfo f = new FileInfo(file.FileName);
            newFname += f.Extension;
            var sPhotoPath = Server.MapPath(photoVPath) + "\\" + newFname;
            var tPhotoPath = Server.MapPath(thumbVPath) + "\\" + newFname;
            file.SaveAs(sPhotoPath); //保存原图
            file.SaveAs(tPhotoPath); //保存缩略图

            var vup = new vUserPhoto
            {
                PhDesc = Request["txtDesc"],
                PhotoName = DateTime.Now.ToString("MMddHHmmss"),
                SourcePath = photoVPath + "/" + newFname,
                ThumbPath = thumbVPath + "/" + newFname,
                UploadTime = DateTime.Now,
                UserID = UserData.UserID,
                ViewCount = 0
            };
            return vup;
        }

        private void LoadUserPhotoes(int userID)
        {
            var ps = UserPhotoMag.Select(userID);
            int count = 0;
            if (ps != null)
                count = ps.Count();
            notice.Visible = (count == 0);
            nextPanel.Visible = !notice.Visible;
            if (count > 0)
            {
                repUserPhotoes.DataSource = ps;
                repUserPhotoes.DataBind();
            }
        }

        protected void repUserPhotoes_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var up = e.Item.DataItem as vUserPhoto;
                if (up == null)
                    return;
                var img = e.Item.FindControl("imgPhoto") as Image;
                img.ImageUrl = up.SourcePath;
                img.ToolTip = up.PhDesc;
                img.Width = 235;
                img.Height = 157;
                System.Drawing.Bitmap s = new System.Drawing.Bitmap(Server.MapPath(img.ImageUrl));
                if (s.Width > s.Height)
                {
                    img.Width = 235;
                    img.Height = 157;
                }
                else
                {
                    img.Width = 115;
                    img.Height = 157;
                }
            }
        }
    }
}